package vaadin.lab4;

import java.io.File;

import com.vaadin.Application;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.ClassResource;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.FileResource;
import com.vaadin.terminal.Resource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Link;
import com.vaadin.ui.Select;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class Lab extends Application {

    private GridLayout mainLayout;
    private Resource currentResource = null;
    private Embedded embedded;

    @Override
    public void init() {
        mainLayout = new GridLayout(3, 2);
        mainLayout.setSpacing(true);
        mainLayout.setMargin(true);
        mainLayout.setWidth("100%");
        mainLayout.setColumnExpandRatio(2, 1.0f);

        Window mainWindow = new Window("Lab", mainLayout);
        setMainWindow(mainWindow);

        buildLayout();
    }

    private void buildLayout() {
        Select resourceTypeSelect = new Select();
        resourceTypeSelect.setImmediate(true);
        resourceTypeSelect.addItem(ClassResource.class);
        resourceTypeSelect.addItem(ExternalResource.class);
        resourceTypeSelect.addItem(FileResource.class);

        final Link link = new Link();
        link.setTargetName("_blank");
        final Button openWindowButton = new Button("Open in this Window",
                new Button.ClickListener() {

                    public void buttonClick(ClickEvent event) {
                        if (currentResource != null) {
                            getMainWindow().open(currentResource);
                        }
                    }
                });
        openWindowButton.setDescription("Open in this application window");
        link.setCaption("Link to the resource");

        mainLayout.addComponent(resourceTypeSelect);
        mainLayout.addComponent(link);
        mainLayout.addComponent(openWindowButton);

        resourceTypeSelect.addListener(new Property.ValueChangeListener() {
            public void valueChange(ValueChangeEvent event) {
                Object value = event.getProperty().getValue();
                currentResource = null;
                if (embedded != null) {
                    mainLayout.removeComponent(embedded);
                }
                embedded = new Embedded("Embedded");
                if (value == FileResource.class) {
                    currentResource = createFileResource();
                    embedded.setType(Embedded.TYPE_BROWSER);
                    embedded.setWidth("500px");
                    embedded.setHeight("500px");
                } else if (value == ClassResource.class) {
                    currentResource = createClassResource();
                    embedded.setType(Embedded.TYPE_IMAGE);
                } else if (value == ExternalResource.class) {
                    currentResource = createExternalResource();
                    embedded.setMimeType("application/x-shockwave-flash");
                    embedded.setType(Embedded.TYPE_OBJECT);
                }

                link.setResource(currentResource);
                embedded.setSource(currentResource);
                mainLayout.addComponent(embedded, 0, 1, 2, 1);
            }
        });
    }

    private Resource createFileResource() {
        File baseDir = getContext().getBaseDirectory();

        return new FileResource(new File(baseDir.getAbsolutePath()
                + "/WEB-INF/files/document.pdf"), this);
    }

    private Resource createExternalResource() {
        return new ExternalResource(
                "http://www.youtube.com/v/QH2-TGUlwu4?version=3&amp;hl=fi_FI");
    }

    private Resource createClassResource() {
        return new ClassResource("pic.jpg", this);
    }
}
